<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>添加失物信息</title>
    <link href="${pageContext.request.contextPath}/css/houtaimain.css" rel="stylesheet" type="text/css" />
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/bootstrap-fileinput.css" rel="stylesheet">
</head>

<body>

<h1 class="h1_style"><i class="i_h1"></i>添加失物信息</h1>
<div class="div_left">


        <input type="text" name="l_id"  id="l_id" value="${lostProduct.l_id}" style="display:none" />
        <p class="form_p"><label class="form_label">物品名称：</label><input type="text" value="${lostProduct.lost_name}"  id="lost_name" name="lost_name" class="form_input readonly " minlength="2"  required ><i class="i_start"></i> <a class="a_note">必填</a> </p>

        <p class="form_p"><label class="form_label">类型：</label>
            <select class="form_select" name="l_tid" id="l_tid" tyle="padding-left: 1%;">
                <c:forEach items="${lostProductTypes}" var="lostProductTypes">
                    <option  value="${lostProductTypes.l_tid}">${lostProductTypes.name}</option>
                </c:forEach>
            </select>
        </p>
        <p class="form_p"><label class="form_label">联系电话：</label><input type="text"  value="${lostProduct.phone}" class="form_input readonly " id="phone" name="phone" minlength="2"  required ><i class="i_start"></i><a class="a_note">请填入真实电话</a> </p>

        <p class="form_p"><label class="form_label a_area">描述：</label><textarea class="text_area1"  value="${lostProduct.describe_txt}"  id="describe_txt" name="describe_txt"  maxlength="150" οnchange=“this.value=this.value.substring(0, 20)” οnkeydοwn=“this.value=this.value.substring(0, 20)” οnkeyup=“this.value=this.value.substring(0, 20)”></textarea></p>
        <div class="clear"></div>
</div>
<div class="container">
    <div class="page-header">
        <h3>图片上传</h3>

        <div class="form-group" id="uploadForm" enctype='multipart/form-data'>

            <div class="fileinput fileinput-new" data-provides="fileinput"  id="exampleInputUpload">
                <div class="fileinput-new thumbnail" style="width: 300px;height: auto;max-height:300px;">
                    <img id='picImg' style="width: 100%;height: auto;max-height: 100%;" src="${pageContext.request.contextPath}/images/noimage.png" alt="" />
                </div>
                <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                <div>
                        <span class="btn btn-primary btn-file">
                            <span class="fileinput-new">选择文件</span>
                            <span class="fileinput-exists">换一张</span>
                            <input type="file" name="file" id="file" accept="image/gif,image/jpeg,image/x-png"/>
                        </span>
                    <a href="javascript:;" class="btn btn-warning fileinput-exists" data-dismiss="fileinput">移除</a>
                </div>
                <p style="color: #a04121">重新上传图片</p>
            </div>
        </div>
    </div>
</div>

<div class="clear"></div>
<p class="but_p"><input type="button" value="提交" class="but_save" onclick="requestJson()"/>



</body>
<script src="${pageContext.request.contextPath}/js/jquery.js"></script>

<script src="${pageContext.request.contextPath}/js/date/js/laydate.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-validation-1.14.0/dist/jquery.validate.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/js/jquery-validation-1.14.0/dist/localization/messages_zh.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap-fileinput.js"></script>
<script>
    !function () {
        laydate.skin('danlan'); //
        laydate({ elem: '#demo' });
        laydate({ elem: '#demo1' }); //
    } ();
</script>
<script >
    $.validator.setDefaults({
        submitHandler: function () {
            alert("修改成功");
        }
    });
    $().ready(function () {
        $(".form_b").validate();
    });
</script>
<script type="text/javascript">
    function  requestJson() {
        //创建一个formData对象实例,(key,val)调用append()方法来添加数据,通过get(key)/getAll(key)来获取对应的value，通过set(key, value)来设置修改数据,通过delete(key)，来删除数据
        var product = new FormData();

        product.append('file', $("#file")[0].files[0]);
        product.append('l_id',$("#l_id").val());
        product.append('lost_name',$("#lost_name").val());
        product.append('l_tid',$("#l_tid").val());
        product.append('phone',$("#phone").val());
        product.append('describe_txt',$("#describe_txt").val());
        if(product.get("lost_name")==""){
            alert("商品名不能为空！");
            return false;
        }
        else if(product.get("l_tid")==""){
            alert("请选择失物类型！");
            return false;
        }
        else if(product.get("phone")==""){
            alert("联系电话不能为空！");
            return false;
        }

        else if(product.get("file")==""){
            alert("商品图片不能为空！");
            return false;
        }
        else if(product.get("describe_txt")==""){
            alert("请添加商品描述，方便他人更好的了解该商品！");
            return false;
        }
        else if(!(/^1[345678]\d{9}$/.test(product.get("phone")))){
            alert("手机号输入有误！");
            return false;
        }

        else
        {$.ajax({

            url: "${pageContext.request.contextPath}/lostProduct/updateLostProduct",

            type: "POST",

            data: product,

            async: false,

            contentType: false,

            processData: false,

            success: function (data) {

                if (data==0) {

                    alert("修改成功！感谢你的拾金不昧")

                    window.location.href ="${pageContext.request.contextPath}/lostProduct/getLostProductByUserId";

                }
                else if (data==-500) {

                    alert("商品图片上传出现问题，请重新上传")

                }

                else if (data==1) {

                    alert("服务器出现故障，请稍后再试")

                }

            },

        });
        }
    }

</script>

</html>
